package com.itheima.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.common.constant.RedisConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.Result;
import com.itheima.converter.CheckGroupConverter;
import com.itheima.converter.SetmealConverter;
import com.itheima.dto.SetmealDTO;
import com.itheima.dto.SetmealPageQueryDTO;
import com.itheima.mapper.SetmealAndCheckGroupMapper;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.CheckGroup;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import com.itheima.vo.SetmealReportVO;
import com.itheima.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.ArrayList;
import java.util.List;

@Service
@Slf4j
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealAndCheckGroupMapper setmealAndCheckGroupMapper;
    @Autowired
    private RedisTemplate redisTemplate;
    /**
     * 新增套餐
     */
    @Override
    @Transactional
    public void saveWithCheckGroup(SetmealDTO setmealDTO, List<String> checkgroupIds) {
        Setmeal setmeal = SetmealConverter.SetmealDTOtoSetmeal(setmealDTO);
        setmealMapper.insert(setmeal);
        //获取insert中的主键值id
        int setmealId = setmeal.getId();
        //转换String集合为Integer集合
        List<Integer> checkGroupIdsInt = new ArrayList<>();
        if (checkgroupIds != null && !checkgroupIds.isEmpty()) {
            for (String checkgroupId : checkgroupIds) {
                Integer checkgroupIdInt = Integer.parseInt(checkgroupId);
                checkGroupIdsInt.add(checkgroupIdInt);
            }
        }
        setmealAndCheckGroupMapper.insert(setmealId,checkGroupIdsInt);
        //将图片存入redis套餐图片保存在数据库中的图片名称集合
        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }

    /**
     * 编辑
     * @param setmealDTO
     * @param checkgroupIds
     */
    @Override
    public void updateWithCheckItem(SetmealDTO setmealDTO, List<String> checkgroupIds) {
        Setmeal setmeal = SetmealConverter.SetmealDTOtoSetmeal(setmealDTO);
        setmealMapper.update(setmeal);
        setmealAndCheckGroupMapper.deleteBysetmealId(setmealDTO.getId());
        List<Integer> checkGroupIdsInt = new ArrayList<>();
        if (checkgroupIds != null && !checkgroupIds.isEmpty()) {
            for (String checkgroupId : checkgroupIds) {
                Integer checkgroupIdInt = Integer.parseInt(checkgroupId);
                checkGroupIdsInt.add(checkgroupIdInt);
            }
        }
        setmealAndCheckGroupMapper.insert(setmeal.getId(),checkGroupIdsInt);
        //将图片存入redis套餐图片保存在数据库中的图片名称集合
        redisTemplate.opsForSet().add(RedisConstant.SETMEAL_PIC_DB_RESOURCES,setmeal.getImg());
    }
    /**
     * 根据id查询
     */
    @Override
    public Setmeal findById(String id) {
        Integer checkId = Integer.parseInt(id);
        Setmeal setmeal =setmealMapper.findById(checkId);
        return setmeal;
    }
    /**
     * 分页查询
     */
    @Override
    public PageResult pageQuery(SetmealPageQueryDTO setmealPageQueryDTO) {
        PageHelper.startPage(setmealPageQueryDTO.getCurrentPage(),setmealPageQueryDTO.getPageSize());
        Page<Setmeal> setmealPage=setmealMapper.pageQuery(setmealPageQueryDTO);
        PageResult pageResult =new PageResult(setmealPage.getTotal(),setmealPage.getResult());
        return pageResult;
    }

    /**
     * 获取套餐占比统计数据
     * @return
     */
    @Override
    public SetmealReportVO getSetmealReport() {
        SetmealReportVO setmealReportVO=new SetmealReportVO();
        List<String>   setmealNameList=new ArrayList<>();
        List<SetmealVO> setmealVO= setmealMapper.findSetMealCount();
        for (SetmealVO smlvo : setmealVO) {
              setmealNameList.add(smlvo.getName());
        }
        setmealReportVO.setSetmealNames(setmealNameList);
        setmealReportVO.setSetmealCount(setmealVO);
        return setmealReportVO;
    }



}
